import rxAjax from '@/assets/ajax.js';

const Api={
    osGroup: '/api-user/user/org/osGroup',
    osUser: '/api-user/user/org/osUser',
    osInst: '/api-user/user/org/osInst',
    osDimension: '/api-user/user/org/osDimension',
    osCommonUser: '/api-user/user/org/osCommonUser',
    osCommonUserGroup: '/api-user/user/org/osCommonUserGroup',
    systemPublic: '/api-system/system/core/public',
    interfaceBaseUrl: '/api-system/system/core/sysInterfaceApi',
    sysFile: '/api-system/system/core/sysFile',
    sysTree: '/api-system/system/core/sysTree',
    sysTreeCat: '/api-system/system/core/sysTreeCat',
    sysDic: '/api-system/system/core/sysDic',
    sysApp: '/api-system/system/core/sysApp',
    sysMenu: '/api-system/system/core/sysMenu',
    sysProperties:"/api-system/system/core/sysProperties",
    sysQrCodeShare:"/api-system/system/core/sysQrCodeShare",
    sysInterfaceApi: '/api-system/system/core/sysInterfaceApi',
    sysInterfaceProject: '/api-system/system/core/sysInterfaceProject',
    sysInterfaceClassification: '/api-system/system/core/sysInterfaceClassification',
    formInvokeScriptUrl: '/api-form/form/core/formInvokeScript',
    formDesign:'/api-form/form/core/formDesign',
    formCustomQueryUrl: '/api-form/form/core/formCustomQuery',
    formQueryStrategy: '/api-form/form/core/formQueryStrategy',
    formTableFieldShow: '/api-form/form/core/formTableFieldShow',
    formAddress: '/api-form/form/share/formAddress',
    formExcelGenTask: '/api-form/form/core/formExcelGenTask',
    formWordTemplate: '/api-form/form/core/formWordTemplate',
    formEntRelation: '/api-form/form/core/formEntRelation',
    formSolution: '/api-form/form/core/formSolution',
    formBoList:'/api-form/form/core/formBoList',
    saveExport: '/api-form/form/core/saveExport',
    formBusinessSolution:'/api-form/form/core/formBusinessSolution',
    formPrintLodop: '/api-form/form/core/formPrintLodop',
    bpmInst:'/api-bpm/bpm/core/bpmInst',
    bpmCheckHistory:'/api-bpm/bpm/core/bpmCheckHistory',
    sysDb:'/api-form/form/core/sysDb',
};

Api.getAjax=function(){
    return rxAjax;
}

Api.pdfPrint=function(parameter) {
    var url= Api.formWordTemplate+'/pdfPrint';
    var config={responseType:'arraybuffer'}
    return rxAjax.download(url,parameter,config);
}

Api.isDeleteByFormSolAlias= function(parameter) {
    return rxAjax.postJson(Api.formEntRelation+'/isDeleteByFormSolAlias',parameter)
}

Api.removeSolutionById = function (parameter) {
    var url = Api.formSolution + '/removeById';
    return rxAjax.postJson(url, parameter);
}
Api.getSaveExportByList = function(dataList){
    var url = Api.saveExport + '/getByList?dataList=' + dataList ;
    return rxAjax.get(url);
}
Api.rowsSolutionSave = function (parameter) {
    var url = Api.formSolution + '/rowsSave';
    return rxAjax.postJson(url, parameter);
}

Api.getSolutionByAlias =function(alias,id,params) {
    var url= Api.formSolution + '/getByAlias?alias=' + alias ;
    if(id){
        url += "&pk=" + id;
    }
    return rxAjax.postJson(url,params);
}
Api.saveFormSolution = function (parameter) {
    var url = Api.formSolution + '/saveForm';
    return rxAjax.postJson(url, parameter);
}
Api.delQueryStrategy = function(parameter){
    var url = Api.formQueryStrategy + '/del';
    return rxAjax.postUrl(url, parameter);
}
Api.saveListFieldCache=function(parameter){
    var url=Api.formTableFieldShow + '/saveListFieldCache';
    return rxAjax.postJson(url,parameter);
}
Api.getBoListEdit2=function(id){
    var url = Api.formBoList + '/getEdit2?id='+id;
    return rxAjax.get(url);
}
Api.boListExport=async function(parameter){
    var canExportUrl=  Api.formBoList + '/canExportExcel';
    var result=await rxAjax.postForm(canExportUrl,parameter);
    if(result.success){
        var url=  Api.formBoList + '/exportExcel';
        var config={responseType:'arraybuffer'}
        parameter.async=result.data?result.data.async:false;
        //异步请求
        if(parameter.async){
            var url=  Api.formBoList + '/asyncExportExcel';
            return rxAjax.postForm(url,parameter).then (res => {
                if(!res){
                    return {
                        async:true,
                        fileId:"",
                        success:false,
                        message:"服务器错误！"
                    }
                }
                res.async=true;
                return res;
            })
        }else {
            return rxAjax.download(url,parameter, config);
        }
    }
}

Api.printHtml=function(parameter){
    var url = Api.formPrintLodop + '/printHtml';
    return rxAjax.postJson(url,parameter);
}

Api.wordPrint=function(parameter) {
    var url= Api.formWordTemplate+'/wordPrint';
    var config={responseType:'arraybuffer'}
    return rxAjax.download(url,parameter,config);
}

Api.removeBusinessSolutionById=function(parameter){
    var url = Api.formBusinessSolution + '/removeById';
    return rxAjax.postJson(url,parameter);
}

Api.queryAddress=function(addressKey,config) {
    let  params={config:JSON.stringify(config),addressKey:addressKey};
    let  url= Api.formAddress + "/queryAddress";
    return rxAjax.postForm(url,params);
}

/**
 * 文件上传
 * @param formData
 * @param callback
 * @returns {*}
 */
Api.upload=function(formData,callback) {
    let  url= Api.sysFile + "/upload";
    return rxAjax.upload(url,formData,callback);
}

Api.getByFileId=function(fileId){
    return rxAjax.get(Api.sysFile + '/getByFileId',{fileId:fileId});
}

//比对nacos上传白名单
Api.getUploadWhiteList=function() {
    var url= Api.sysProperties+'/getUploadWhiteList';
    return rxAjax.get(url);
}

//部门控件查询
Api.searchGroups= function(parameter){
    return rxAjax.postJson(Api.osGroup + '/searchGroups',parameter)
}

/**
 * 获取下一级部门列表
 * @param parentId
 * @returns {AxiosPromise}
 */
Api.getParentGroup= function(parentId,initRankLevel) {
    return rxAjax.get(Api.osGroup + '/getByParentId?parentId='+parentId+'&initRankLevel='+initRankLevel)
}

/*
* getDownGroups
* 获取当前选中的用户组的所有子孙节点的数据
* */
Api.getDownGroups = function (groupId){
    let url = Api.osGroup + `/getDownGroups?groupId=${groupId}`;
    return rxAjax.get(url);
}

/**
 * 获取组织维度列表
 * @returns {AxiosPromise}
 */
Api.getDimList= function(tenantId) {
    var params={};
    if(tenantId){
        params.tenantId=tenantId;
    }
    return rxAjax.get(Api.osDimension + '/getDimList',params)
}
Api.getByDimIds=function(dimIds){
    return rxAjax.get(Api.osDimension + '/getByDimIds?dimIds='+dimIds,{})
}

/**
 * 获取指定用户组ID列表
 * @param groupId
 * @param initRankLevel
 */
Api.getOsGroupByGroupId=function(groupId,initRankLevel) {
    let url = `${Api.osGroup}/getOsGroupByGroupId?groupId=${groupId}`
    if(initRankLevel){
        url += `&initRankLevel=${initRankLevel}`
    }
    return rxAjax.get(url)
}

/**
 * 根据维度ID获取部门列表
 * @param parameter
 * @returns {AxiosPromise}
 */
Api.getGroupListByDimId= function(parameter) {
    return rxAjax.postJson(Api.osGroup + '/getGroupListByDimId',parameter)
}

// 根据用户id集合批量获取主部门路径
Api.getMainDepsByUserIds= function(userIds) {
    return rxAjax.get(Api.osUser + '/getMainDepsByUserIds?userIds='+userIds)
}

/**
 * 获取排除指定用户组ID列表
 * @param groupId
 * @param initRankLevel
 */
Api.getOsGroupByExcludeGroupId=function(groupId,dimId,initRankLevel,parentId,tenantId) {
    var params={groupId:groupId,dimId:dimId,parentId:parentId};
    if(initRankLevel){
        params.initRankLevel=initRankLevel;
    }
    if(tenantId){
        params.tenantId= tenantId;
    }
    return rxAjax.get(Api.osGroup + '/getOsGroupByExcludeGroupId',params)
}
/**
 * 查询用户组
 * @param dimId
 * @param initRankLevel
 * @param parentId
 * @param tenantId
 * @returns {*}
 */
Api.queryGroups=function(dimId,initRankLevel,parentId,tenantId){
    var url=Api.osGroup + '/queryGroups?dimId='+dimId+"&parentId="+parentId;
    if(initRankLevel){
        url+="&initRankLevel="+initRankLevel;
    }
    if(tenantId){
        url+="&tenantId="+tenantId;
    }
    return rxAjax.get(url);
}
//获取满足条件的流程定义ID
Api.checkCanChange = function (parameter){
    var url =Api.formDesign+"/checkCanChange";
    return rxAjax.postJson(url,parameter);
}
Api.executeInterfaceApi=function(apiKey,params){
    return rxAjax.postJson(Api.interfaceBaseUrl + '/executeApi?apiKey='+apiKey,params);
}
Api.invokeScript=function(alias,params){
    return rxAjax.postJson(Api.formInvokeScriptUrl + '/invoke/'+alias,params);
}
Api.queryForJson=function(sqlKey,params){
    return rxAjax.postForm(Api.formCustomQueryUrl + '/queryForJson_'+sqlKey,params);
}
Api.getCheckHistorys=function (instId) {
    var url= Api.bpmCheckHistory+"/getCheckHistorys?instId="+ instId;
    return rxAjax.get(url);
}
Api.startProcess=function(parameter){
    parameter.checkType="AGREE";
    let url = Api.bpmInst+ '/startProcess';
    return rxAjax.postJson(url,parameter);
}
Api.saveDraft=function(parameter){
    let url = Api.bpmInst+ '/saveDraft';
    return rxAjax.postJson(url,parameter);
}
/**
 * 根据流程实例ID进行加密操作
 * @param instId
 * @returns {*}
 */
Api.getBpmInstEncrypt=function(instId){
    let url =Api.bpmInst+ '/getBpmInstEncrypt';
    return rxAjax.postForm(url,{instId:instId});
}
Api.getQueryStrategyByListId = function(listId){
    var url = Api.formQueryStrategy + '/getByListId?listId=' + listId;
    return rxAjax.postJson(url);
}
Api.saveQueryStrategyByList = function(parameter){
    var url = Api.formQueryStrategy + '/saveRowsByList';
    return rxAjax.postJson(url,parameter);
}
Api.getQueryStrategy = function(pkId){
    var url = Api.formQueryStrategy + '/get?pkId=' + pkId;
    return rxAjax.get(url);
}
//记录下载
Api.downloadRecord=function(parameter){
    var url =Api.formExcelGenTask + '/downloadRecord';
    return rxAjax.postForm(url,parameter)
}
//获取异步下载进度信息
Api.getProgress=function(pkId){
    var url =Api.formExcelGenTask + '/get?pkId='+pkId;
    return rxAjax.get(url);
}

/**
 * 数据字典
 * @param appId
 * @returns {*}
 */
Api.listDicTree =function(appId) {
    let url= Api.sysTree+"/listDicTree";
    let params={};
    if(appId){
        params.appId=appId;
    }
    return rxAjax.get(url,params);
}

Api.loadDic=function(dicKey) {
    var url= Api.sysDic+"/listByKey";
    var params={key:dicKey};
    return rxAjax.get(url,params);
}

/**
 * 获取第三方接口
 * @param appId
 * @param isShare
 * @returns {*}
 */
Api.queryProjectByAppId =function(appId,isShare) {
    let url=Api.sysInterfaceProject+'/queryProjectByAppId?appId=' + appId + '&isShare=' + isShare
    return rxAjax.get(url);
}

/**
 * 获取分类接口
 * @param projectId
 * @returns {*}
 */
Api.queryClassificationByProjectId =function(projectId) {
    let url= Api.sysInterfaceClassification+'/queryByProjectId?projectId='+projectId
    return rxAjax.get(url);
}

/**
 * 接口定义
 * @param projectId
 * @returns {*}
 */
Api.queryApiByProjectId =function(projectId) {
    let url=Api.sysInterfaceApi+'/queryByProjectId?projectId='+projectId+'&status=DONE'
    return rxAjax.get(url);
}

/**
 * 接口定义
 * @param pkId
 * @returns {*}
 */
Api.queryApiByApiId =function(pkId) {
    let url=Api.sysInterfaceApi+"/get?pkId="+pkId
    return rxAjax.get(url);
}

Api.getTreeCatByKey= function (catKey,isI18=true) {
    return rxAjax.get(Api.sysTreeCat + '/getTreeCatByKey?catKey='+catKey+'&isI18='+isI18)
}

Api.getCatByParentId= function(parentId,catKey,readKey,isAdmin,isGrant,statisticConfig,isBpmClient,appId,isMyBpm) {

    let params={parentId:parentId,catKey:catKey,readKey:readKey,isAdmin:isAdmin,isGrant:isGrant};
    if(isBpmClient){
        params.isBpmClient=isBpmClient;
    }
    if(isMyBpm){
        params.isMyBpm=isMyBpm;
    }
    if(appId){
        params.appId=appId;
    }
    setStatistConfig(params,statisticConfig);

    return rxAjax.postForm(Api.sysTree + '/getByParentId',params)
}

Api.getTopByCatKey= function(catKey,readKey,isAdmin,isGrant,appId,statisticConfig,isBpmClient,isMyBpm) {
    let params= {catKey:catKey,readKey:readKey,isAdmin:isAdmin,isGrant:isGrant,appId:appId};
    if(isBpmClient){
        params.isBpmClient=isBpmClient;
    }
    if(isMyBpm){
        params.isMyBpm=isMyBpm;
    }
    setStatistConfig(params,statisticConfig);

    return rxAjax.postForm(Api.sysTree + '/getTopNodesByCatKey',params)
}

function setStatistConfig(params, config){
    if(config && config!="undefined"){
        params.statisticConfig=config
    }
}


Api.getByCatKey =function(catKey,readKey,isAdmin,isGrant,appId,statisticConfig) {
    let params={catKey:catKey,readKey:readKey, isAdmin:isAdmin,isGrant:isGrant,appId:appId,isI18:true};
    if(statisticConfig && statisticConfig!="undefined"){
        params.statisticConfig=statisticConfig
    }
    return rxAjax.postForm(Api.sysTree+'/getByCatKey',params)
}

Api.getParamsEncrypt=function(){
    var url = Api.systemPublic+'/getParamsEncrypt';
    return rxAjax.get(url);
}
//选择人员查询输入拼音或者中文查询 人员数据
Api.searchUserData = function (parameter){
    let url = Api.osUser+'/getUser';
    return rxAjax.postJson(url,parameter);
}
//选择部门/组织查询输入拼音或者中文查询 部门/组织数据
Api.searchGroupData = function (parameter){
    let url = Api.osGroup+'/getGroup';
    return rxAjax.postJson(url,parameter);
}

//查询常用人分组
Api.queryCommonUserGroup = function (parameter){
    let url = Api.osCommonUserGroup+'/query';
    return rxAjax.postJson(url,parameter);
}
//保存常用人分组
Api.saveCommonUserGroup = function (parameter){
    let url = Api.osCommonUserGroup+'/save';
    return rxAjax.postJson(url,parameter);
}

//删除常用人分组
Api.delCommonUserGroupById = function (id){
    let url = Api.osCommonUserGroup+'/delById?id='+id;
    return rxAjax.get(url);
}

//保存常用人
Api.saveCommonUsers = function (parameter){
    let url = Api.osCommonUser+'/saveCommonUsers';
    return rxAjax.postJson(url,parameter);
}

//查询常用人
Api.getAllUserByCommonGroupId = function (parameter){
    let url = Api.osUser+'/getAllUserByCommonGroupId';
    return rxAjax.postJson(url,parameter);
}

//删除常用人分组
Api.delByCommonGroupId = function (parameter){
    let url = Api.osCommonUser+'/delByCommonGroupId';
    return rxAjax.postJson(url,parameter);
}

Api.getUserInfo= function(userId) {
    var url= Api.osUser + '/getUserInfo?userId=' + userId;
    return rxAjax.get(url);
}

Api.getOsUser=function(userId){
    var url= Api.osUser + '/get?pkId=' + userId;
    return rxAjax.get(url);
}

Api.getOsGroup=function(groupId){
    var url= Api.osGroup + '/get?pkId=' + groupId;
    return rxAjax.get(url);
}

Api.getOsInst=function(instId){
    var url= Api.osInst + '/get?pkId=' + instId;
    return rxAjax.get(url);
}
Api.setFormBoListConf=function(parameter){
    var url = Api.formBoList + '/' + parameter.key + '/config';
    return rxAjax.get(url);
}
Api.dialog=function(conf){
    var url= Api.formBoList + '/' + conf.key +'/dialog';
    if(conf.params){
        url+="?" + conf.params;
    }
    if(conf.menuId){
        if(url.indexOf("?")==-1){
            url+="?menuId=" +conf.menuId;
        } else{
            url+="&menuId=" +conf.menuId;
        }
    }
    if(conf.pmtAlias){
        if(url.indexOf("?")==-1){
            url+="?pmtAlias=" +conf.pmtAlias;
        } else{
            url+="&pmtAlias=" +conf.pmtAlias;
        }
    }
    if(conf.single){
        if(url.indexOf("?")==-1){
            url+="?single=" +conf.single;
        } else{
            url+="&single=" +conf.single;
        }
    }
    return rxAjax.get(url);
}

Api.getRowsShortUrl=function(parameter){
    return rxAjax.postJson(Api.sysQrCodeShare + '/getRowsShortUrl',parameter);
}
Api.getSysAppByAppId=function(appId){
    var url = Api.sysApp +  '/getByAppId?appId=' + appId;
    return rxAjax.get(url);
}
Api.reloadColumnsByKey=function(key){
    var url = Api.formBoList + '/reloadColumnsByKey?key='+key;
    return rxAjax.get(url);
}
Api.saveConfigJson=function(parameter){
    var url = Api.saveExport + '/saveConfigJson';
    return rxAjax.postJson(url,parameter);
}
Api.delSaveExport=function(parameter){
    var url = Api.saveExport + '/del';
    return rxAjax.postUrl(url,parameter);
}
Api.getPrivateByList = function(dataList){
    var url = Api.saveExport + '/getPrivateByList?dataList=' + dataList ;
    return rxAjax.get(url);
}
Api.boListImportExcel=function(parameter){
    var url = Api.formBoList + '/importExcel';
    return rxAjax.upload(url,parameter);
}
Api.getBoListById=function(pkId) {
    var url= Api.formBoList + '/get?pkId=' + pkId;
    return rxAjax.get(url);
}
Api.validPreConditionByList=function(preCondConfig,rows){
    var url = Api.formBoList + '/validPreByButton';
    rows=rows || [];
    return rxAjax.postJson(url,{config:preCondConfig,rows:rows});
}
/**
 * 获取后端用户与路由菜单信息
 */
Api.getLoginUserAndMenu=function(appKey) {
    let url = Api.sysMenu+"/getMenusByAppKey?appKey=" + appKey;
    return rxAjax.get(url);
}

//获取后端脚本方法列表
Api.getScriptClasses=function() {
    let url = "/api-form/form/core/formInvokeScript/getScriptClasses";
    return rxAjax.get(url);
}
//查询表
Api.findTableList= function(parameter){
    return rxAjax.postForm(Api.sysDb + '/findTableList',parameter)
}

//系统常量
Api.getConstants= function(){
    return rxAjax.get( '/api-form/form/core/formCustomQuery/getConstantItem')
}

//获取常用脚本分类
Api.getCommonScriptByUse=function(useTO,type) {
    return Api.getAjax().get('/api-form/form/core/formCommonScript/getCommonScriptByUse?useTO='+useTO+"&type="+type,{})
}


//获取共享的数据字典
Api.queryShareDicListByAppId = function(appId){
    var url ='/api-form/form/core/formResourceShare/queryShareDicListByAppId?appId=' + appId;
    return rxAjax.get(url);

}

// 获取流程
Api.bpmInstQuery=function(params) {
    var url= '/api-bpm/bpm/core/bpmInst/query'
    return rxAjax.postJson(url,params);
}
// 我的申请
Api.myBpmInsts=function(params) {
    var url= '/api-bpm/bpm/core/bpmInst/myBpmInsts'
    return rxAjax.postJson(url,params);
}
// 我的代办
Api.myTasks=function(params) {
    var url= '/api-bpm/bpm/core/bpmTask/myTasks'
    return rxAjax.postJson(url,params);
}
// 我的已办
Api.getMyApprovedInsts=function(params) {
    var url= '/api-bpm/bpm/core/bpmInst/getMyApprovedInsts'
    return rxAjax.postJson(url,params);
}
// 我收到的转发
Api.getMyRecevieTurn=function(params) {
    var url= '/api-bpm/bpm/core/bpmInstCc/myRecevieTurn'
    return rxAjax.postJson(url,params);
}

//根据类型获取印章列表
Api.getStampList=function (type){
    let url ="/api-form/form/core/formStamp/getStamps";
    let params;
    if(type){
        params={"type":type};
    }
    return rxAjax.postForm(url,params);
}
export default Api;
